----------------------------
-- Simplistic Street
-- by Arran
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

function FindPlayer(info)
	local pls = player.GetAll()

	-- Find by Index Number (status in console)
	for k, v in pairs(pls) do
		if tonumber(info) == v:UserID() then
			return v
		end
	end

	-- Find by Steam ID
	for k, v in pairs(pls) do
		if info == v:SteamID() then
			return v
		end
	end
	
	-- Find by Partial Nick
	for k, v in pairs(pls) do
		if string.find(string.lower(v:Name()), string.lower(tostring(info)), 1, true) ~= nil then
			return v
		end
	end
	return nil
end

function LogEvent(Text)
	local Date = string.gsub(os.date("%x"), "/", "-")
	local FName = "Logs/"..Date..".txt"
	Text = "("..os.date("%X")..") "..Text
	if(file.Exists(FName)) then
		local LastText = file.Read(FName)
		Text = LastText..Text..".\n"
	else
		Text = Text..".\n"
	end
	file.Write(FName, Text)
end

function PrintMessageAll( type, msg, ic )
	
	if( ic == nil ) then ic = false; end
	ic = !ic;
	
	for k, v in pairs( player.GetAll() ) do
		v:PrintMessage( type, msg, ic );
	end

end


function ccKick( ply, cmd, args )

	local target = FindPlayer(args[1]);
	
	if not ply:IsAdmin() then
	
		ply:PrintMessage( 2, "You cannot use this command!" );
		ply:PrintMessage( 3, "You cannot use this command!" );
		return
	end		
	
	if target and not target:IsSuperAdmin() then	
	
		local reason = "";
	
		for n = 2, #args do
		
			reason = reason .. args[n] .. " ";
		
		end
		
		reason = "Kicked by " .. ply:Nick() .. " for: " .. reason;
		
		if( reason == "" ) then
			reason = "Kicked by " .. ply:Nick();
		end
	
		LogEvent("#"..ply:Nick().." kicked "..target:Nick().." for the reason: "..reason)
		PrintMessageAll( 3, ply:Nick() .. " kicked " .. target:Nick() );
		
		game.ConsoleCommand( "kickid " .. target:UserID() .. " \"" .. reason .. "\"\n" );
	
	end
	
	if target:IsSuperAdmin() then 
	
	ply:PrintMessage( 2, "You cannot kick a Superadmin." );
	
	end
	
	if not target then
	
		ply:PrintMessage( 2, "This user does not exist!" );
		ply:PrintMessage( 3, "This user does not exist!" );
	end	
end
concommand.Add( "utopia_kick", ccKick );

function ccBan( ply, cmd, args )

	local target = FindPlayer(args[1]);

	if not ply:IsAdmin() then
	
		ply:PrintMessage( 2, "You cannot use this command!" );
		ply:PrintMessage( 3, "You cannot use this command!" );
		return
	end
	
	if target then	
		if !args[2] then 
			ply:PrintMessage( 2, "utopia_ban <name> <hours> <minutes> - Ban a player")
			ply:PrintMessage( 3, "utopia_ban <name> <hours> <minutes> - Ban a player") 
			return 
		end
	
	if not (tonumber(args[3]) < 60) then
			ply:PrintMessage( 2, "You cannot have more than 60 minutes! You entered ".. args[3].." minutes.")
			ply:PrintMessage( 3, "You cannot have more than 60 minutes! You entered ".. args[3].." minutes.") 		
			return
		end
	
		local Hours = ( tonumber( args[2] )*60 ) *60
		local Minutes = tonumber( ( args[3] or "0" ) )*60
		local Seconds = Hours + Minutes
		--local amt = tonumber( ( args[2] or "0" ) )
		
		
		LogEvent("#"..ply:Nick().." has banned "..target:Nick().." for ".. args[2] .. " hours and ".. (args[3] or 0) .." minutes. (SteamID: ".. target:SteamID()..")")
		
		if( file.Exists( "../cfg/banned_user.cfg" ) ) then
			game.ConsoleCommand( "exec banned_user.cfg\n" );
		end
	
		PrintMessageAll( 3, ply:Nick() .. " banned " .. target:Nick() );
	
		game.ConsoleCommand( "banid " .. Seconds .. " " .. target:SteamID() .. "\n" );
		game.ConsoleCommand( "kickid " .. target:UserID() .. " \"Banned for " .. args[2] .. ".".. args[3].. " hours by " .. ply:Nick() .. "\"\n" );
			
	end
	
	if not target then
	
		ply:PrintMessage( 2, "This user does not exist!" );
		ply:PrintMessage( 3, "This user does not exist!" );
	
	end

end

concommand.Add( "utopia_ban", ccBan );

function ccResetMap( ply, cmd, args )

	if not ply:IsAdmin() then
	
		ply:PrintMessage( 2, "You cannot use this command!" );
		ply:PrintMessage( 3, "You cannot use this command!" );
		return
	end

	for k, v in pairs( player.GetAll() ) do
	
		v:PrintMessage( HUD_PRINTTALK, ply:Nick().. " is restarting the map." );
		LogEvent("#"..ply:Nick().." restarted the map")
	
	end
	
	timer.Simple( 5, game.ConsoleCommand, "changelevel " .. game.GetMap() .. "\n" );

end
concommand.Add( "utopia_resetmap", ccResetMap );

function ccChangeMap( ply, cmd, args )

	if not ply:IsAdmin() then
	
		ply:PrintMessage( 2, "You cannot use this command!" );
		ply:PrintMessage( 3, "You cannot use this command!" );
		return
	end

	local Maps = {}
	local Map = file.Find("../maps/*.bsp")
	for _, MapV in pairs(file.Find("../maps/*.bsp")) do
		if(string.find(MapV, "_")) then
			Maps[#Maps + 1] = MapV:sub(1, -5)
		end
	end
	table.sort(Maps)
  
	local FoundIt = false
	for k, v in pairs(Maps) do
		if(string.find(args[1], v)) then
			FoundIt = true
			PrintMessageAll( 3, ply:Nick().." has changed the map to "..args[1])
			LogEvent("#"..ply:Nick().."has changed the map to "..args[1])
			game.ConsoleCommand("changelevel "..v.."\n")
		end
	end
	if(FoundIt == false) then
		ply:PrintMessage( 2, "This map does not exist!" );
		ply:PrintMessage( 3, "This map does not exist!" );
	end
end

concommand.Add( "utopia_changemap", ccChangeMap );

function ccAdmin( ply, cmd, args )

	if not ply:IsSuperAdmin() then
	
		ply:PrintMessage( 2, "You cannot use this command!" );
		ply:PrintMessage( 3, "You cannot use this command!" );
		return
	end

	local target = FindPlayer(args[1]);
	
	if(target == false) then
		ply:PrintMessage( 2, "This player does not exist!" );
		ply:PrintMessage( 3, "This player does not exist!" );
		
	elseif(ply:IsUserGroup("admin") == true) then
		ply:PrintMessage( 2, "This player is already an admin!" );
		ply:PrintMessage( 3, "This player is already an admin!" );
		
	else
		target:SetUserGroup("admin")
		PrintMessageAll( 3, ply:Nick().." has made "..target:Nick().." an admin.")
		LogEvent("#"..ply:Nick().." has made "..target:Nick().." an admin.")
	end
end

concommand.Add( "utopia_admin", ccAdmin );

function ccRemoveAdmin( ply, cmd, args )

	if not ply:IsSuperAdmin() then
	
		ply:PrintMessage( 2, "You cannot use this command!" );
		ply:PrintMessage( 3, "You cannot use this command!" );
		return
	end

	local target = FindPlayer(args[1]);
	
	if(target == false) then
		ply:PrintMessage( 2, "This player does not exist!" );
		ply:PrintMessage( 3, "This player does not exist!" );
		
	elseif(target == ply) then
		ply:PrintMessage( 2, "You cannot remove your own privilages!" );
		ply:PrintMessage( 3, "You cannot remove your own privilages!" );
		
	elseif(target:IsUserGroup("") == true) then
		ply:PrintMessage( 2, "You cannot revoke this user's privilages!" );
		ply:PrintMessage( 3, "You cannot revoke this user's privilages!" );		
	else
		target:SetUserGroup("")
		PrintMessageAll( 3, ply:Nick().." removed "..target:Nick().."'s privilages.")
		LogEvent("#"..ply:Nick().." removed "..target:Nick().." privileges")
	end
end

concommand.Add( "utopia_removeadmin", ccRemoveAdmin );